<?php


namespace app\erp\controller;


use app\erp\model\Pikuanxinxidb;

class Pikuanxinxi extends Base
{
    protected $cols = array(
        array(
            'field' => 'pkxx_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
        ),

        array(
            'field' => 'fkdw',
            'width' => 400,
            'title' => '付款单位'
        ),
        array(
            'field' => 'address',
            'width' => 400,
            'title' => '地址'
        ),
        array(
            'field' => 'phone',
            'width' => 160,
            'title' => '手机号'
        ),
        array(
            'field' => 'xxp',
            'width' => 300,
            'title' => '销项票'
        ),

        array(
            'fixed' => 'right',
            'field' => 'oprate',
            'width' => 120,
            'toolbar' => '#pikuanxinxi_tool',
            'title' => '操作'
        ),

    );

    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $pikuanxinxiDb = new Pikuanxinxidb();
        $pikuanxinxiDb->param = $this->post;
        $get_list = $pikuanxinxiDb
            ->order('pkxx_id desc')
            ->paginate($this->post['limit']);
        $this->layuiReturn($get_list->items(), $get_list->total());
    }

    public function tplForm()
    {
        $pkxx_id = 0;
        $data = array();
        if (isset($this->post['id'])) {
            $pikuanxinxiDb = new Pikuanxinxidb();
            $data = $pikuanxinxiDb->find($this->post['id']);
            $pkxx_id = $this->post['id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('pkxx_id', $pkxx_id);
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $pikuanxinxiDb = new Pikuanxinxidb();
        if (isset($this->post['pkxx_id']) && $this->post['pkxx_id']) {
            $get_pkxx = $pikuanxinxiDb->find($this->post['pkxx_id']);
            $get_pkxx->save($this->post);
        } else {
            $pikuanxinxiDb->save($this->post);
        }

        $this->layuiReturn('保存成功');
    }

    /**
     * Title("删除")
     */
    public function ajaxDelete()
    {
        $xinxidb = new Pikuanxinxidb();
        if (is_array($this->post['pkxx_id'])) {
            $xinxidb->destroy($this->post['pkxx_id']);
        } else {
            $get_xinxi = $xinxidb->find($this->post['pkxx_id']);
            if ($get_xinxi) {
                $get_xinxi->delete();
            }
        }
        $this->layuiReturn('删除成功');
    }
}